package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierDao supplierDao;

    //查询供应商
    @Override
    public List<Supplier> getSupplierList(Integer page, Integer rows,String supplierName) {
        List<Supplier> list=supplierDao.selectSupplierList(page,rows,supplierName);
        return list;
    }

    //添加修改供应商
    @Override
    public ServiceVO saveOrUpdate(Supplier supplier) {
        if (supplier.getSupplierId()==null){//id为空说明是新增
            //判断用户名是否注册过
            String supplierName = supplier.getSupplierName();
            Supplier supplier1= supplierDao.selectSupplierByName(supplierName);
            if (supplier1!=null){
                return new ServiceVO(ErrorCode.ROLE_EXIST_CODE,ErrorCode.ROLE_EXIST_MESS);//角色已存在
            }
           supplierDao.insertSupplier(supplier);

        }else {//有id传过来说明是修改操作
            /*String supplierName = supplier.getSupplierName();
            Supplier supplier1= supplierDao.selectSupplierByName(supplierName);*/
            String supplierName = supplier.getSupplierName();
            Supplier supplier1= supplierDao.selectSupplierByName(supplierName);
            if (supplier1!=null){
                return new ServiceVO(ErrorCode.ROLE_EXIST_CODE,ErrorCode.ROLE_EXIST_MESS);//角色已存在
            }
            supplierDao.updateSupplier(supplier);

        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    //删除供应商
    @Override
    public void deleteById(Integer supplierId) {
        supplierDao.deleteById(supplierId);
    }

    @Override
    public List<Supplier> getComboboxList(String q) {
        return supplierDao.getComboboxList(q);
    }


}
